﻿/******************************************************************
** 文件名:Sys_OperatorDAL.cs
** Copyright (c) 
** 创建人:佟礼
** 日 期:2012-05-27 13:15:42
** 描 述:数据操作类
********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using Enter.DataProxy.DbConnector;
using Enter.Entity;
using Enter.DataProxy.Mail.Base;
namespace Enter.DataProxy.Mail
{
    /// <summary>
    /// 数据操作类：Sys_OperatorManager
    /// </summary>
    public class Sys_OperatorManager : DataProxyManager, ISys_OperatorManager
    {
        public Sys_OperatorManager() :
            base(ProxyProvider.ConfigurationName, ProxyProvider.ConfiruationDbName) { }
        #region 插入记录
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <returns></returns>
        public int Insert(Sys_OperatorEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            var db = base.CreateDbWriter();
            strSql.Append("INSERT INTO Sys_Operator");
            strSql.Append("(LoginName,UserName,Password,DeptID,EMail,Sex,Mobile,Address,Memo,RoleList,Status)");
            strSql.Append(" values ");
            strSql.Append("(@LoginName,@UserName,@Password,@DeptID,@EMail,@Sex,@Mobile,@Address,@Memo,@RoleList,@Status)");
            ParameterCollection parameters = CreateParameter();
            parameters.Append("LoginName", model.LoginName, DbType.String);
            parameters.Append("UserName", model.UserName, DbType.String);
            parameters.Append("Password", model.Password, DbType.String);
            parameters.Append("DeptID", model.DeptID, DbType.String);
            parameters.Append("EMail", model.EMail, DbType.String);
            parameters.Append("Sex", model.Sex, DbType.Int32);
            parameters.Append("Mobile", model.Mobile, DbType.String);
            parameters.Append("Address", model.Address, DbType.String);
            parameters.Append("Memo", model.Memo, DbType.String);
            parameters.Append("RoleList", model.RoleList, DbType.String);
            parameters.Append("Status", model.Status, DbType.Int32);
            return db.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public int Update(Sys_OperatorEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            var db = base.CreateDbWriter();
            strSql.Append("UPDATE Sys_Operator SET ");
            strSql.Append("LoginName=@LoginName,UserName=@UserName,Password=@Password,DeptID=@DeptID,EMail=@EMail,Sex=@Sex,Mobile=@Mobile,Address=@Address,Memo=@Memo,RoleList=@RoleList,Status=@Status");
            strSql.Append(" WHERE ID=@ID");
            ParameterCollection parameters = CreateParameter();
            parameters.Append("ID", model.ID, DbType.Int32);
            parameters.Append("LoginName", model.LoginName, DbType.String);
            parameters.Append("UserName", model.UserName, DbType.String);
            parameters.Append("Password", model.Password, DbType.String);
            parameters.Append("DeptID", model.DeptID, DbType.String);
            parameters.Append("EMail", model.EMail, DbType.String);
            parameters.Append("Sex", model.Sex, DbType.Int32);
            parameters.Append("Mobile", model.Mobile, DbType.String);
            parameters.Append("Address", model.Address, DbType.String);
            parameters.Append("Memo", model.Memo, DbType.String);
            parameters.Append("RoleList", model.RoleList, DbType.String);
            parameters.Append("Status", model.Status, DbType.Int32);
            return db.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete(int ID)
        {
            var db = base.CreateDbWriter();
            string strSql = "DELETE Sys_Operator WHERE ID=@ID";
            ParameterCollection parameters = CreateParameter();
            parameters.Append("ID", ID);
            return db.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion

        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public Sys_OperatorEntity FillDetailWithReader(IDataReader reader)
        {
            Sys_OperatorEntity model = new Sys_OperatorEntity();
            if (reader["ID"] != DBNull.Value)
                model.ID = (int)reader["ID"];
            if (reader["LoginName"] != DBNull.Value)
                model.LoginName = (string)reader["LoginName"];
            if (reader["UserName"] != DBNull.Value)
                model.UserName = (string)reader["UserName"];
            if (reader["Password"] != DBNull.Value)
                model.Password = (string)reader["Password"];
            if (reader["DeptID"] != DBNull.Value)
                model.DeptID = (string)reader["DeptID"];
            if (reader["EMail"] != DBNull.Value)
                model.EMail = (string)reader["EMail"];
            if (reader["Sex"] != DBNull.Value)
                model.Sex = (Sys_OperatorEntity.EnumSex)(byte)reader["Sex"];
            if (reader["Mobile"] != DBNull.Value)
                model.Mobile = (string)reader["Mobile"];
            if (reader["Address"] != DBNull.Value)
                model.Address = (string)reader["Address"];
            if (reader["Memo"] != DBNull.Value)
                model.Memo = (string)reader["Memo"];
            if (reader["RoleList"] != DBNull.Value)
                model.RoleList = (string)reader["RoleList"];
            if (reader["Status"] != DBNull.Value)
                model.Status = (Sys_OperatorEntity.EnumStatus)(byte)reader["Status"];
            return model;
        }
        #endregion

        #region 根据ID获取一个对象
        public Sys_OperatorEntity GetObjectByID(int ID)
        {
            string sql = "SELECT TOP 1 * FROM Sys_Operator  WHERE ID=@ID";
            var db = base.CreateDbReader();
            ParameterCollection parameters = CreateParameter();
            parameters.Append("ID", ID, DbType.Int32);
            try
            {
                IDataReader reader = db.ExecuteReader(sql, parameters);
                if (reader.Read())
                {
                    return this.FillDetailWithReader(reader);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }
        #endregion

        #region 数据集
        public IList<Sys_OperatorEntity> ShowList()
        {
            var db = base.CreateDbReader();
            string strSql = "SELECT  * FROM Sys_Operator";
            IList<Sys_OperatorEntity> result = null;
            result = db.Execute<Sys_OperatorEntity>(strSql);
            return result;
        }
        #endregion

        #region 数据集
        public DataTable ShowTable()
        {
            var db = base.CreateDbReader();
            string strSql = "SELECT  * FROM Sys_Operator";
            return db.ExecuteDataSet(strSql.ToString()).Tables[0];
        }
        #endregion

        #region 分页数据集
        public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
        {
            BaseManager manager = new BaseManager();
            entity.tbName = "Sys_Operator";
            DataTable dt = manager.GetTable(entity);
            totalPages = entity.TotalPages;
            totalCount = entity.TotalCount;
            return dt;
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public Sys_OperatorEntity GetObjectByLoginName(string loginName)
        {
            string sql = "SELECT TOP 1 * FROM Sys_Operator  WHERE LoginName=@loginName";
            var db = base.CreateDbReader();
            ParameterCollection parameters = CreateParameter();
            parameters.Append("loginName", loginName, DbType.String);
            try
            {
                IDataReader reader = db.ExecuteReader(sql, parameters);
                if (reader.Read())
                {
                    return this.FillDetailWithReader(reader);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }
    }
}
